package jjcache

import (
	"jjcache/jjcache/strategy"
	"sync"
)

type cache struct {
	mu         sync.Mutex
	memSg      string
	sg         strategy.Strategy
	cacheBytes int64
}

func (c *cache) add(key string, value ByteView) {
	c.mu.Lock()
	defer c.mu.Unlock()
	if c.sg == nil {
		if c.memSg == "lfu" {
			c.sg = strategy.NewLFUCache(c.cacheBytes, nil)
		} else {
			c.sg = strategy.NewLRUCache(c.cacheBytes, nil)
		}
	}
	c.sg.Add(key, value)
}

func (c *cache) get(key string) (value ByteView, ok bool) {
	c.mu.Lock()
	defer c.mu.Unlock()

	if c.sg == nil {
		return
	}

	if v, ok := c.sg.Get(key); ok {
		return v.(ByteView), ok
	}

	return
}
